package com.sweetcircle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sweetcircle.annotation.CheckObject;
import com.sweetcircle.annotation.CheckValue;
import com.sweetcircle.annotation.GlobalInterceptor;
import com.sweetcircle.domain.po.ContactApply;
import com.sweetcircle.domain.po.UserContact;
import com.sweetcircle.domain.vo.ContactApplyVO;
import com.sweetcircle.domain.vo.ResponseVO;
import com.sweetcircle.exception.BusinessException;
import com.sweetcircle.service.ContactApplyService;
import com.sweetcircle.service.UserContactService;
import com.sweetcircle.websocket.MessageHandler;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


/**
 * &#064;project_name: sweet-circle-server
 * &#064;Author: Anan
 * &#064;time: 2024/7/29 下午8:09
 */

@RestController
@RequestMapping("/contact")
public class ContactController extends ABaseController {

    private static final Logger log = LoggerFactory.getLogger(ContactController.class);
    @Resource
    private UserContactService userContactService;
    @Resource
    private ContactApplyService contactApplyService;

    @Resource
    private MessageHandler messageHandler;


    /**
     * 获取联系人列表
     *
     * @param params
     * @return
     */
    @PostMapping("/contactList")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO contactList(@CheckValue(required = true) @RequestBody HashMap<String, String> params) {
        List<HashMap<String, Object>> allContact = userContactService.getAllContact(params.get("userId"));
        return successResponseVO(allContact);
    }


    /**
     * 添加联系人  发送好友添加申请
     *
     * @param contactApplyVO
     * @return
     */
    @PostMapping("/addContact")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO addContact(@CheckObject(required = true, checkProperties = {"sweetCircleAuth", "sweetCircleStatusB", "sweetCircleStatusA", "userId", "contactUserId"}) @RequestBody ContactApplyVO contactApplyVO) {
        return userContactService.addContact(contactApplyVO);
    }

    /**
     * 获取联系人申请列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/contactApplyList")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO getContactApply(@CheckValue(required = true) String userId) {
        List<ContactApply> one = contactApplyService.list(new LambdaQueryWrapper<ContactApply>()
                .eq(ContactApply::getUserId, userId)
                .eq(ContactApply::getStatus, 1));
        return successResponseVO(one);
    }


    /**
     * 删除联系人申请
     *
     * @param userId
     * @return
     */
    @PostMapping("/deleteContactApply")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO deleteContactApply(@CheckValue(required = true) String contactApplyId, @CheckValue(required = true) String userId) {

        boolean update = contactApplyService.remove(new LambdaQueryWrapper<ContactApply>()
                .eq(ContactApply::getContactApplyId, contactApplyId));
        if (!update) {
            throw new BusinessException(400, "删除联系人申请失败");
        }
        List<ContactApply> all = contactApplyService.list(new LambdaQueryWrapper<ContactApply>()
                .eq(ContactApply::getUserId, userId)
                .eq(ContactApply::getStatus, 1));
        return successResponseVO(all);
    }

    /**
     * 同意添加联系人
     *
     * @param userContact
     * @return
     */
    @PostMapping("/acceptApply")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO acceptApply(@RequestBody HashMap<String, UserContact> userContact) {
        List<HashMap<String, Object>> allContact = userContactService.acceptApply(userContact);
        return successResponseVO(allContact);
    }

    /**
     * 更新联系人备注
     *
     * @param userContact
     * @return
     */
    @PostMapping("/updateContactNoteName")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO updateContactNoteName(@CheckObject(required = true, checkProperties = {"userId", "contactUserId"}) @RequestBody UserContact userContact) {
        List<HashMap<String, Object>> allContact = userContactService.updateContactNoteName(userContact);
        return successResponseVO(allContact);
    }

    @PostMapping("/updateContactInfo/sweetCircleAuth")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO updateContactInfo(@CheckObject(required = true, checkProperties = {"userId", "contactUserId", "sweetCircleAuth", "sweetCircleStatusA", "sweetCircleStatusB"}) @RequestBody UserContact userContact) {
        List<HashMap<String, Object>> allContact = userContactService.updateContactInfo(userContact);
        return successResponseVO(allContact);
    }

    @PostMapping("/joinInBlacklist")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO joinInBlacklist(@CheckObject(required = true, checkProperties = {"userId", "contactUserId"}) @RequestBody UserContact userContact) {

        List<HashMap<String, Object>> allContact = userContactService.joinInBlacklist(userContact);
        return successResponseVO(allContact);
    }


    @PostMapping("/removeBlacklist")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO removeBlacklist(@CheckObject(required = true, checkProperties = {"userId", "contactUserId"}) @RequestBody UserContact userContact) {

        List<HashMap<String, Object>> allContact = userContactService.removeBlacklist(userContact);
        return successResponseVO(allContact);

    }

    @PostMapping("/deleteContact")
    @GlobalInterceptor(checkParams = true, checkLogin = true)
    public ResponseVO deleteContact(@CheckObject(required = true, checkProperties = {"userId", "contactUserId"}) @RequestBody UserContact userContact) {
        boolean list = userContactService.deleteContact(userContact);
        return successResponseVO("删除联系人成功");
    }


}
